﻿using System;
using System.Data;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.MODULE.Controls
{
    public partial class AddEditModule : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                if (string.IsNullOrEmpty(Request.QueryString["mdid"]))
                {
                    // new module definition
                    txtModuleName.Text = "[Tên module]";
                    txtSourceFile.Text = "~/Modules/[Module Folder]/Main.ascx";
                }
                else
                {
                    // Obtain the module definition to edit from the database
                    PortalConfiguration config = new PortalConfiguration();
                    Guid mdid = new Guid(Request.QueryString["mdid"]);
                    PortalDataSet.ModuleDefinitionRow moduleDefinitionRow = config.GetSingleModuleDefinition(mdid);

                    // Read in information
                    txtModuleDefinitionID.Text = moduleDefinitionRow.ID.ToString();
                    txtModuleName.Text = moduleDefinitionRow.Name;
                    txtSourceFile.Text = moduleDefinitionRow.SourceFile;

                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            PortalConfiguration config = new PortalConfiguration();

            if (this.ItemID == 0)
            {
                // Obtain Portal from Current Context
                Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)Context.Items["Portal"];

                // Add a new module definition to the database
                config.AddModuleDefinition(portal.ID, new Guid(txtModuleDefinitionID.Text), txtModuleName.Text, txtSourceFile.Text);
            }
            else
            {
                // update the module definition
                config.UpdateModuleDefinition(new Guid(txtModuleDefinitionID.Text), txtModuleName.Text, txtSourceFile.Text);
            }
            this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            PortalConfiguration config = new PortalConfiguration();
            //config.DeleteModuleDefinition(this.ItemID);

            // Go back.
            this.ReturnToReferrerPage();
        }

        protected void btnReadXml_Click(object sender, EventArgs e)
        {
            try
            {
                string xmlPath = Server.MapPath(txtXmlPath.Text) + "/install.xml";
                DataSet ds = new DataSet();
                ds.ReadXml(xmlPath);
                DataRow row = ds.Tables[0].Rows[0];
                txtModuleDefinitionID.Text = row["ID"].ToString();
                txtModuleName.Text = row["Name"].ToString();
                txtSourceFile.Text = row["SourceFile"].ToString();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
    }
}